#ifndef _UTIL_CONFIGURE_H_
#define _UTIL_CONFIGURE_H_
#include "common/common.h"

EXTERN_C_BEGIN

typedef struct _config_item_t	config_item_t;
typedef struct _config_group_t config_group_t;
typedef struct _config_t config_t;

/**
 * parse a config file and store it in structure config_t.
 * @param file the config file path
 * @return point of config_t, it is malloc, must be destroyed by destroy_config(). NULL if failed.
 * @ref void destroy_config(config_t *).
 */
extern config_t * parse_config_file(const char * file);
/**
 * parse a string and store it in structure config_t.
 * @param string the config string.
 * @return point of config_t, it is malloc, must be destroyed by destroy_config(). NULL if failed.
 * @ref void destroy_config(config_t *).
 */
extern config_t * parse_config_string(const char * str);
/**
 * destroy a config_t point which is generated by parse_config_file() or parse_config_string().
 * @param cfg config_t point generated by parse_config_file() or parse_config_string().
 * @ref void parse_config_file(const char *).
 * @ref void parse_config_string(const char *).
 */
extern void destroy_config(config_t * cfg);
/**
 * get the root group of config_t.
 * @param cfg config_t point generated by parse_config_file() or parse_config_string().
 * @return point of config_group_t, the root group of cfg.
 * @ref void parse_config_file(const char *).
 * @ref void parse_config_string(const char *).
 */
extern config_group_t * get_root_config_group(config_t * cfg);
/**
 * find the group named as 'name' in cfg.
 * @param cfg config_t point generated by parse_config_file() or parse_config_string().
 * @param name group name, it can be a multi-name.
 *      'a/b/c' means root->a->b->c, '->' means get child.
 * @return point of config_group_t, the group named as 'name' in cfg.
 * @ref void parse_config_file(const char *).
 * @ref void parse_config_string(const char *).
 */
extern config_group_t * find_config_group(config_t * cfg, const char * name);
/**
 * find the group named as 'name' in p's brothers.
 * @param p point of config_group_t.
 * @param name group name, cann't be a multi-name.
 * @return point of config_group_t, the group named as 'name' in p's brothers.
 */
extern config_group_t * find_sibling_config_group(config_group_t * p, const char * name);
/**
 * find the group named as 'name' in p's children.
 * @param p point of config_group_t.
 * @param name group name, cann't be a multi-name.
 * @return point of config_group_t, the group named as 'name' in p's children.
 */
extern config_group_t * find_child_config_group(config_group_t * p, const char * name);
/**
 * get the value of key in grp.
 * @param grp point of config_group_t.
 * @param key key name.
 * @return value the value of key.
 */
extern config_group_t * get_sibling_config_group(config_group_t * p);
extern config_group_t * get_child_config_group(config_group_t * p);
extern const char * get_config_value(config_group_t * grp, const char * key);
extern void first_config_item(config_group_t * grp);
extern BOOL next_config_item(config_group_t * grp, const char ** key, const char ** val);

extern void display_config(config_t * cfg);
extern void display_config_group(config_group_t * grp, const char * surfix);

EXTERN_C_END

#endif //_UTIL_CONFIGURE_H_
